import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    practiceHistory: []
  },
  getters: {
    allPracticeHistory: state => state.practiceHistory
  },
  mutations: {
    setPracticeHistory(state, history) {
      state.practiceHistory = history
    },
    addPracticeRecord(state, record) {
      state.practiceHistory.push(record)
    }
  },
  actions: {
    loadPracticeHistory({ commit }) {
      // 从本地存储加载练习历史
      const history = JSON.parse(localStorage.getItem('practiceRecords') || '[]')
      commit('setPracticeHistory', history)
    },
    savePracticeRecord({ commit }, record) {
      // 保存到本地存储
      const history = JSON.parse(localStorage.getItem('practiceRecords') || '[]')
      history.push(record)
      localStorage.setItem('practiceRecords', JSON.stringify(history))
      commit('addPracticeRecord', record)
    }
  }
})